//
//  TTSetViewController.m
//  TaoTuo
//
//  Created by admin on 2019/10/16.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTSetViewController.h"
#import "TTCellArrowModel.h"
#import "TTCellLabelArrowModel.h"
#import "TTCellSwitchModel.h"
#import "TTCellImageArrowModel.h"
#import "TTCellLabelModel.h"
#import "TTCommonViewCell.h"
#import <SDWebImage.h>
#import "TTInvitationCodeViewController.h"
#import "TTFeedbackViewController.h"

static NSString *cellID = @"set_cell_id";

@interface TTSetViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    TTCellLabelArrowModel *_clearCacheModel;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation TTSetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    [self setupDatas];
    [self setupBottomView];
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSMutableArray *arr = self.dataArray[section];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *arr = self.dataArray[indexPath.section];
    TTCellModel *model = arr[indexPath.row];
    TTCommonViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *arr = self.dataArray[indexPath.section];
    TTCellModel *model = arr[indexPath.row];
    if ([self respondsToSelector:model.selector]) {
        IMP imp = [self methodForSelector:model.selector];
        void(*func)(id, SEL) = (void *)imp;
        func(self, model.selector);
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 10)];
    return view;
}

- (void)setupDatas
{
    @weakify(self)
    SDImageCache *cache = [SDImageCache sharedImageCache];
    [cache calculateSizeWithCompletionBlock:^(NSUInteger fileCount, NSUInteger totalSize) {
        @strongify(self)
        NSLog(@"--> %ld, %ld", fileCount, totalSize);
        // 缓存总大小
         CGFloat size = (CGFloat)totalSize / (1024 * 1024);
        NSString *string = [NSString stringWithFormat:@"%.1fM", size];
        self->_clearCacheModel.content = string;
        [self.tableView reloadData];
    }];
    
//    NSString *notifyKey = @"notify_key";
//    NSString *voiceKey = @"voice_key";
//    NSString *vibrationKey = @"vibration_key";
    
    NSMutableArray *arr1 = [NSMutableArray array];
    [arr1 addObject:[TTCellArrowModel setModelWithTitle:@"用户注册协议" selector:@selector(lookRegisterProtocol)]];
    
    NSMutableArray *arr2 = [NSMutableArray array];
//    [arr2 addObject:[TTCellSwitchModel setModelWithTitle:@"新信息通知" key:notifyKey isOpen:[USER_DEFAULT boolForKey:notifyKey] changeBlock:^(BOOL on) {
//        NSLog(@"新信息通知 ---> %d", on);
//    }]];
//    [arr2 addObject:[TTCellSwitchModel setModelWithTitle:@"声音" key:voiceKey isOpen:[USER_DEFAULT boolForKey:voiceKey] changeBlock:^(BOOL on) {
//           NSLog(@"声音 ---> %d", on);
//       }]];
//    [arr2 addObject:[TTCellSwitchModel setModelWithTitle:@"震动" key:vibrationKey isOpen:[USER_DEFAULT boolForKey:vibrationKey] changeBlock:^(BOOL on) {
//           NSLog(@"震动 ---> %d", on);
//       }]];
    
    _clearCacheModel = [TTCellLabelArrowModel setModelWithTitle:@"清除缓存" content:nil selector:@selector(clearCache) contentColor:UIColorHex(666666)];
    [arr2 addObject:_clearCacheModel];
    
    NSMutableArray *arr3 = [NSMutableArray array];
    [arr3 addObject:[TTCellImageArrowModel setModelWithTitle:@"邀请二维码" imageName:@"invitationqrcode" selector:@selector(lookInvitationCode)]];
//    [arr3 addObject:[TTCellArrowModel setModelWithTitle:@"我要反馈" selector:@selector(gotoFeedback)]];
    [arr3 addObject:[TTCellLabelModel setModelWithTitle:@"版本" content:kCurrentVersion contentColor:UIColorHex(666666)]];
    
    [self.dataArray addObject:arr1];
    [self.dataArray addObject:arr2];
    [self.dataArray addObject:arr3];
    
    [self.tableView reloadData];
}

// 注册协议
- (void)lookRegisterProtocol
{
    NSString *urlString = [NSString stringWithFormat:@"%@html/Agreement/", kH5BaseURL];
    LLWebViewController *webVC = [[LLWebViewController alloc] init];
    [webVC setUrlString:urlString htmlString:nil];
    webVC.webTitle = @"注册协议";
    [self.navigationController pushViewController:webVC animated:YES];
}

// 清除缓存
- (void)clearCache
{
    [MBProgressHUD showHUDWithMessage:@"缓存清理中..."];
    SDImageCache *cache = [SDImageCache sharedImageCache];
    [cache clearMemory];    // 同步清除内存缓存
    // 清理磁盘缓存
    [cache clearDiskOnCompletion:^{
        [MBProgressHUD hiddenHUD];
        sleep(0.5);
        [MBProgressHUD showSuccess:@"清理完成"];
        self->_clearCacheModel.content = @"0.0M";
        [self.tableView reloadData];
    }];
    
    // 删除开屏广告缓存
    [USER_DEFAULT removeObjectForKey:ad_data_key];
    //删除公告id
    [USER_DEFAULT removeObjectForKey:notifyID_key];
    [USER_DEFAULT synchronize];
}

// 邀请二维码
- (void)lookInvitationCode
{
    TTInvitationCodeViewController *invitationVC = [[TTInvitationCodeViewController alloc] init];
    [self.navigationController pushViewController:invitationVC animated:YES];
}

// 反馈
- (void)gotoFeedback
{
    TTFeedbackViewController *feedbackVC = [[TTFeedbackViewController alloc] init];
    [self.navigationController pushViewController:feedbackVC animated:YES];
}

// 退出登录
- (void)logoutAction
{
    LLSystemAlertView *alertView = [LLSystemAlertView alertViewWithTitle:@"提示" message:@"您确定要退出登录吗？" cancelTitle:@"取消" cancelBlock:^{} confirmTitle:@"确定" confirmBlock:^BOOL{
        
        [TTLoginSingle deleteUserInfo];
        [[TTLoginSingle sharedSingle].loginSubject sendNext:@(0)];
        
        [self presentLoginViewController];
        
        [self.navigationController popToRootViewControllerAnimated:YES];
        
        return YES;
    }];
    
    [TYShowAlertView showAlertViewWithView:alertView backgoundTapDismissEnable:YES];
}

- (void)setupBottomView
{
    UIButton *logoutBtn = [UIButton createButtonWithTitle:@"退出登录" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:18] backgroundColor:kMainColor target:self action:@selector(logoutAction)];
    [self.view addSubview:logoutBtn];
    [logoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottomLayoutGuideTop);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(45 * kScale);
    }];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = kSeparatorColor;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 50;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
        _tableView.separatorColor = kSeparatorColor;
        [_tableView registerClass:[TTCommonViewCell class] forCellReuseIdentifier:cellID];
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.mas_topLayoutGuideTop);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuideTop).mas_offset(-45 * kScale);
        }];
    }
    return _tableView;
}



- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


@end
